#!/bin/bash
###
 # @Author: dvlproad
 # @Date: 2023-04-29 15:23:47
 # @LastEditors: dvlproad
 # @LastEditTime: 2023-05-24 17:04:09
 # @Description: 
### 
# sh check_branch_can_pack_env.sh ${FILE_PATH}
:<<!
检查本分支是否可以打包指定环境
!

FILE_PATH=$1 # "app_info.json"

User=`whoami`

JQ_EXEC=`which jq`


echo "$0检查本分支是否可以打包指定环境"

# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
#bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute}/..
bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..
#echo "bulidScriptCommon_dir_Absolute=${bulidScriptCommon_dir_Absolute}"

source ${bulidScriptCommon_dir_Absolute}/a_function.sh ${bulidScriptCommon_dir_Absolute} # 为了使用 updatePackageErrorCodeAndMessage
check_jsonFile_valid ${FILE_PATH}


BRANCH=$(cat $FILE_PATH | ${JQ_EXEC} .package_from_brance | sed 's/\"//g')
PlatformType=$(cat $FILE_PATH | ${JQ_EXEC} .platform_type | sed 's/\"//g')
PackageTargetType=$(cat $FILE_PATH | ${JQ_EXEC} .package_default_target | sed 's/\"//g')
PackageNetworkType=$(cat $FILE_PATH | ${JQ_EXEC} .package_default_env | sed 's/\"//g')
echo "BRANCH=$BRANCH,PlatformType=$PlatformType,PackageTargetType=$PackageTargetType,PackageNetworkType=$PackageNetworkType"
ShortBranchName=${BRANCH##*/} #修复远程分支 origin/dev_will_publish 判断失误问题

function checkBranchAllow() {
    lastProductBranchAllowNameArray=$1
    #echo "lastProductBranchAllowNameArray=${lastProductBranchAllowNameArray[*]}"
    
    lastProductBranchAllowNameCount=${#lastProductBranchAllowNameArray[@]}
    #echo "lastProductBranchAllowNameCount=${lastProductBranchAllowNameCount}"
    if [ ${lastProductBranchAllowNameCount} = 0 ]; then
        echo "${PackageTargetType}人群的${PackageNetworkType}环境的打包不需要限制打包分支，将正常继续执行"
    else
        echo "${PackageTargetType}人群的${PackageNetworkType}环境的打包有需要限制打包分支，将先执行检查"

        if [[ "${lastProductBranchAllowNameArray[*]}" =~ ${ShortBranchName} ]]; then
            echo "${PackageTargetType}人群的${PackageNetworkType}环境的打包可使用${BRANCH}分支进行"
        else
            PackageErrorCode=-1
            PackageErrorMessage="${PackageTargetType}人群的${PackageNetworkType}环境的打包不可使用${BRANCH}分支，请检查您选择的分支,使用${lastProductBranchAllowNameArray[*]}之一"        
            updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
            sh noti_new_package.sh -appInfoF ${FILE_PATH} -ll "${PackageErrorMessage}" --log-robottype "error"
            return 1
        fi
    fi
    
    return 0
}

# 检查分支是否可以用于打包指定环境包
if [ "${PackageNetworkType}" == "product" ] || [ "${PackageNetworkType}" == "preproduct" ] ; then
    if [[ ${PackageTargetType} == "formal" ]] ; then    # appstore人群
        lastProductBranchAllowNameArray=("master" "development" "dev_publish_out" "dev_publish_in") # 外测生产包允许的分支(额外添加dev_publish_in是因为大楼的内测包有时候需要传testFlight)
    elif [[ ${PackageTargetType} == "inner" ]] ; then   # 大楼人群
        lastProductBranchAllowNameArray=("dev_publish_in" "dev_publish_out")
    elif [[ ${PackageTargetType} == "dev" ]] ; then     # 15楼人群
        lastProductBranchAllowNameArray=("dev_publish_in")
    fi
    
    checkBranchAllow ${lastProductBranchAllowNameArray}
    if [ $? != 0 ]; then
        echo "抱歉❌:${PackageNetworkType}环境的打包不可以使用${BRANCH}分支"
        exit_script
    fi
    echo "恭喜💐:${PackageNetworkType}环境的打包可以使用${BRANCH}分支"
else
    echo "恭喜💐:${PackageNetworkType}环境的打包不限制分支"
fi
